package com.itheima_1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class RflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //method();
       // method1();
        //meothd2();
        Class aClass = Class.forName("com.itheima_1.Student");
        Constructor declaredConstructor = aClass.getDeclaredConstructor(String.class);
        //被private修饰不能直接使用
        //如果要用就要强行获取并使用，需要取消访问检查
        declaredConstructor.setAccessible(true);
        Student o =(Student) declaredConstructor.newInstance("zhangsan");
        System.out.println(o);
    }

    public static void meothd2() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class aClass = Class.forName("com.itheima_1.Student");
        Student o =(Student) aClass.newInstance();
        System.out.println(o);
    }

    public static void method1() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class aClass = Class.forName("com.itheima_1.Student");
        Constructor constructor = aClass.getConstructor();
        Student o = (Student) constructor.newInstance();
        System.out.println(o);
    }

    public static void method() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class aClass = Class.forName("com.itheima_1.Student");
        Constructor constructor = aClass.getConstructor(String.class,int.class);
        Object o = constructor.newInstance("张三",23);
        System.out.println(o);
    }
}
